﻿using System;
using System.Collections;
using ApplicationGenerator.BusinessModule.Interfaces;

namespace ApplicationGenerator.BusinessModule.MetaElements
{
    /// <summary>
    /// Kolekcja elementów meta słownikowych
    /// </summary>
    internal class MetaDictionaryItemCollection : DictionaryBase, IMetaDictionaryItemCollection
    {
        public void Add(IMetaDictionaryItem metaDictionaryItem)
        {
            try
            {
                Dictionary.Add(metaDictionaryItem.DictionaryKey, metaDictionaryItem.DictionaryValue);
            }
            catch
            {
                throw new ArgumentException("Nie można zarejestrować w kolekcji pól słownikowych elementów o takich samych nazwach: "
                    + metaDictionaryItem.DictionaryKey + " (" + metaDictionaryItem.DictionaryValue + ")");
            }
        }

        public bool Contains(IMetaDictionaryItem metaDictionaryItem)
        {
            return Dictionary.Contains(metaDictionaryItem);
        }

        public string this[string dictionaryKey]
        {
            get { return Dictionary[dictionaryKey] as string; }
        }

        public string GetKeyByValue(string dictionaryValue)
        {
            string returnKey = null;

            foreach (object dictionaryKey in Dictionary.Keys)
            {
                if ((string)Dictionary[dictionaryKey] == dictionaryValue)
                {
                    returnKey = dictionaryKey as string;
                }
            }

            return returnKey;
        }

        #region IEnumerable Members

        public new IEnumerator GetEnumerator()
        {
            return Dictionary.Values.GetEnumerator();
        }

        #endregion
    }

}
